/**
This file is part of Studentport.

   Studentport is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   Studentport is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with Studentport.  If not, see <http://www.gnu.org/licenses/>.

 **/

package com.groupalpha.studentport;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;

import com.httpclasses.HtmlHelper;
import com.httpclasses.Vobject;

/**
 * 
 * Will present the user with an webview loaded with htmlstring fetched from
 * HtmlHelper.behandlaGasque() method.
 * 
 * @author GroupAlpha
 * 
 */
public class Busses extends Activity {

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.busstops);
		final TableLayout table = (TableLayout) findViewById(R.id.tableLayout1);
		TextView view = (TextView) findViewById(R.id.textView1);

		final Toast toast = Toast.makeText(Busses.this,
				"det uppstod problem vid anslutningen", Toast.LENGTH_SHORT);
		final ProgressDialog dialog = new ProgressDialog(Busses.this);
		dialog.show();
		Bundle extras = getIntent().getExtras();
		Boolean eng = extras.getBoolean("Eng");
		final Button loggoff = (Button) findViewById(R.id.logoff);
		final int black = Color.BLACK;
		final ArrayList<TableRow> rowsInTable = new ArrayList<TableRow>();

		if (eng)
			dialog.setMessage("Loading busses...");
		else
			dialog.setMessage("Laddar bussar...");

		String color = "";

		if (eng)
			loggoff.setBackgroundResource(R.drawable.logoff_button_en);
		final int select = extras.getInt("Dest");
		if (select == 0)
			view.setText("Lindholmen");
		else
			view.setText("Chalmers");

		loggoff.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				error();

			}
		});
		Thread T = new Thread() {
			public void run() {
				try {
					final List<Vobject> v = HtmlHelper.getBusses(select);
					int i = 0;
					if (!v.isEmpty())
						for (Vobject p : v) {
							final Vobject q = p;
							TableRow row = new TableRow(Busses.this);
							TextView id = new TextView(Busses.this);
							TextView tripTime = new TextView(Busses.this);
							TextView nexttripTime = new TextView(Busses.this);
							TextView dest = new TextView(Busses.this);
							dest.setPadding(5, 0, 0, 0);
							TextView island = new TextView(Busses.this);

							id.setText(p.getLineId());
							id.setTextSize((float) 12);

							tripTime.setText(p.getNextTrip());
							tripTime.setTextSize((float) 12);

							nexttripTime.setText(p.getNextNextTrip());
							nexttripTime.setTextSize((float) 12);

							dest.setText(p.getDest());
							dest.setTextSize((float) 12);

							island.setText(p.getIsland());
							island.setTextSize((float) 12);
							Log.d("Color", p.getColor());

							int color = Color.parseColor(p.getColor().trim());

							id.setBackgroundColor(color);
							if (i % 2 == 0) {

								id.setTextColor(Color.WHITE);
								tripTime.setTextColor(Color.WHITE);
								nexttripTime.setTextColor(Color.WHITE);
								dest.setTextColor(Color.WHITE);
								island.setTextColor(Color.WHITE);

								nexttripTime.setBackgroundColor(black);
								dest.setBackgroundColor(black);
								island.setBackgroundColor(black);
								tripTime.setBackgroundColor(black);
							} else {

								id.setTextColor(Color.WHITE);
								tripTime.setTextColor(Color.BLACK);
								nexttripTime.setTextColor(Color.BLACK);
								dest.setTextColor(Color.BLACK);
								island.setTextColor(Color.BLACK);

								nexttripTime.setBackgroundColor(Color.WHITE);
								dest.setBackgroundColor(Color.WHITE);
								island.setBackgroundColor(Color.WHITE);
								tripTime.setBackgroundColor(Color.WHITE);
							}
							row.addView(id);
							row.addView(dest);
							row.addView(island);
							row.addView(tripTime);
							row.addView(nexttripTime);
							row.setPadding(0, 2, 0, 0);
							id.setGravity(Gravity.CENTER);
							rowsInTable.add(row);

							i++;

						}

					Busses.this.runOnUiThread(new Runnable() {
						public void run() {
							if (!rowsInTable.isEmpty())
								for (TableRow row : rowsInTable)
									table.addView(row);

							dialog.dismiss();
						}
					});
				} catch (StringIndexOutOfBoundsException e) {
					Log.w("error", e);
					dialog.dismiss();
					toast.show();
				} catch (Exception e) {
					Log.d("error", "", e);

					dialog.dismiss();
					toast.show();

				}

			}
		};
		T.start();

	}

	public void error() {
		Intent intent = new Intent(Busses.this, StudentportActivity.class);
		intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); // Removes
															// other
															// Activities
															// from
															// stack
		startActivity(intent);

	}

}